import { useParams } from "react-router-dom";
import { Component, initSchema } from "@/stores/edit-pages-store";
import { getPage } from "@/interfaces";
import { useEffect, useState } from "react";
import { Preview } from "./components/Preview";
import { Empty } from "antd";
export default function LegoPage() {
  const { path } = useParams();
  const [apiSchema, setApiSchema] = useState<Component>();
  const [pageState, setPageState] = useState(false);
  const getSchema = async (path: string) => {
    const res = await getPage(path);
    const page = res.data.data;
    if (page) {
      const schema = JSON.parse(res.data.data.schema) || initSchema();
      setApiSchema(schema);
    }
    setPageState(!!page);
  };
  useEffect(() => {
    path && getSchema(path);
  }, [path]);
  return pageState ? (
    <Preview apiSchema={apiSchema} />
  ) : (
    <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="页面走丢了" />
  );
}
